package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwaxT;
import cn.e4j.modular.crm.service.GwaxTService;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 产品配件明细管理
 * @author liuw
 * @date 2019/09/04
 */
@Controller
@RequestMapping("/crm/gwai501")
public class Gwai501Controller extends BaseController {

    @Autowired
    private GwaxTService gwaxTService;

    /**
     * 跳转属性特征值管理页面
     */
    @RequiresPermissions("crm:gwai501:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai501.html";
    }

    /**
     * 查询属性特征值信息列表
     */
    @RequestMapping("/getList")
    @RequiresPermissions("crm:gwai501:init")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWAXENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> result = gwaxTService.getList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwai501:init")
    @ResponseBody
    public ResponseData add(GwaxT gwaxT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        // 判断编号是否重复
        GwaxT gwax = gwaxTService.getByKey(shiroUser.getDBC(), gwaxT.getGWAX001(), gwaxT.getGWAX002());
        if (gwax != null) {
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_CODE.getCode(),
                    CrmExceptionEnum.EXISTED_THE_CODE.getMessage());
        }
        gwaxTService.add(gwaxT);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwai501:init")
    @ResponseBody
    public ResponseData edit(GwaxT gwaxT) {
        gwaxTService.edit(gwaxT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwai501:init")
    @ResponseBody
    public ResponseData del(GwaxT gwaxT) {
        gwaxTService.delete(gwaxT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("crm:gwai501:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwaxT> gwaxTList = inDto.getReqList(GwaxT.class);
        gwaxTService.delDataBatch(gwaxTList);
        return SUCCESS_TIP;
    }

    /**
     * 获取产品资料列表
     */
    @ResponseBody
    @RequestMapping("/getGwamList")
    public Dto getGwamList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWAMENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> list = gwaxTService.getGwamList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    /**
     * 获取产品单位列表
     */
    @ResponseBody
    @RequestMapping("/getGwabList")
    public Dto getGwabList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWABENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> list = gwaxTService.getGwabList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    /**
     * 新增时获取最大该产品配件列表的最大项次
     */
    @ResponseBody
    @RequestMapping("/getMaxGwax002")
    public Dto getMaxGwax002(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWAXENT", shiroUser.getDBC());
        Integer maxGwax002 = gwaxTService.getMaxGwax002(inDto);
        if(maxGwax002 == null) {
            outDto.put("maxGwax002", 1);
        }else {
            outDto.put("maxGwax002", maxGwax002 + 1);
        }
        return outDto;
    }
}
